<?php
namespace app\vueapi\controller;

use app\common\model\{
    Semester as SemesterModel , Week as WeekModel
};

use think\Db;

class Semester extends Base
{
    public function index()
    {
        $data = $this->request->param();
        $number = $data['week'];
        $where['laboratory_id'] = $data['laboratory_id'];
        $field = 'semester_'.$number;
        $semester = SemesterModel::where($where)->field($field)->find();
        $day['id'] = $semester[$field];
        $week = WeekModel::where($day)->find();
        if(!empty($week)) {
            $res = $week;
        }else {
            $res = [
                'id' => null,
                'fri' => null,
                'mon' => null,
                'sat' => null,
                'sun' => null,
                'thu' => null,
                'tue' => null,
                'wed' => null
            ];
        }
        return json(msg(1, $res));
    }
    public function edit()
    {
        $data = $this->request->param();
        if(!empty($data['id'])){
            $id['id'] = $data['id'];
            $week = WeekModel::where($id)->find();
            if($data['week'] == 0) {
                $res = json_encode($data['new_edit']);
                $week -> mon = $res;
                $week -> save();
            }
            if($data['week'] == 1) {
                $res = json_encode($data['new_edit']);
                $week -> tue = $res;
                $week -> save();
            }
            if($data['week'] == 2) {
                $res = json_encode($data['new_edit']);
                $week -> wed = $res;
                $week -> save();
            }
            if($data['week'] == 3) {
                $res = json_encode($data['new_edit']);
                $week -> thu = $res;
                $week -> save();
            }
            if($data['week'] == 4) {
                $res = json_encode($data['new_edit']);
                $week -> fri = $res;
                $week -> save();
            }
            if($data['week'] == 5) {
                $res = json_encode($data['new_edit']);
                $week -> sat = $res;
                $week -> save();
            }
            if($data['week'] == 6) {
                $res = json_encode($data['new_edit']);
                $week -> sun = $res;
                $week -> save();
            }
        }else {
            if($data['week'] == 0) {
                $edit = json_encode($data['new_edit']);
                $res['mon'] = $edit;
                $WeekModel = WeekModel::insert($res);
                $where['mon'] = $edit;
                $field = 'id,mon';
                $semester = WeekModel::where($where)->field($field)->find();
                $number = 'semester_'.$data['week_id'];
                $wherelaboratory['laboratory_id'] = $data['laboratory_id'];
                $week = SemesterModel::where($wherelaboratory)->find();
                if(!empty($week)) {
                    $week -> $number = $semester['id'];
                    $week -> save();
                }
            }
            if($data['week'] == 1) {
                $edit = json_encode($data['new_edit']);
                $res['tue'] = $edit;
                $WeekModel = WeekModel::insert($res);
                $where['tue'] = $edit;
                $field = 'id,tue';
                $semester = WeekModel::where($where)->field($field)->find();
                $number = 'semester_'.$data['week_id'];
                $wherelaboratory['laboratory_id'] = $data['laboratory_id'];
                $week = SemesterModel::where($wherelaboratory)->find();
                if(!empty($week)) {
                    $week -> $number = $semester['id'];
                    $week -> save();
                }
            }
            if($data['week'] == 2) {
                $edit = json_encode($data['new_edit']);
                $res['wed'] = $edit;
                $WeekModel = WeekModel::insert($res);
                $where['wed'] = $edit;
                $field = 'id,wed';
                $semester = WeekModel::where($where)->field($field)->find();
                $number = 'semester_'.$data['week_id'];
                $wherelaboratory['laboratory_id'] = $data['laboratory_id'];
                $week = SemesterModel::where($wherelaboratory)->find();
                if(!empty($week)) {
                    $week -> $number = $semester['id'];
                    $week -> save();
                }
            }
            if($data['week'] == 3) {
                $edit = json_encode($data['new_edit']);
                $res['thu'] = $edit;
                $WeekModel = WeekModel::insert($res);
                $where['thu'] = $edit;
                $field = 'id,thu';
                $semester = WeekModel::where($where)->field($field)->find();
                $number = 'semester_'.$data['week_id'];
                $wherelaboratory['laboratory_id'] = $data['laboratory_id'];
                $week = SemesterModel::where($wherelaboratory)->find();
                if(!empty($week)) {
                    $week -> $number = $semester['id'];
                    $week -> save();
                }
            }
            if($data['week'] == 4) {
                $edit = json_encode($data['new_edit']);
                $res['fri'] = $edit;
                $WeekModel = WeekModel::insert($res);
                $where['fri'] = $edit;
                $field = 'id,fri';
                $semester = WeekModel::where($where)->field($field)->find();
                $number = 'semester_'.$data['week_id'];
                $wherelaboratory['laboratory_id'] = $data['laboratory_id'];
                $week = SemesterModel::where($wherelaboratory)->find();
                if(!empty($week)) {
                    $week -> $number = $semester['id'];
                    $week -> save();
                }
            }
            if($data['week'] == 5) {
                $edit = json_encode($data['new_edit']);
                $res['sat'] = $edit;
                $WeekModel = WeekModel::insert($res);
                $where['sat'] = $edit;
                $field = 'id,sat';
                $semester = WeekModel::where($where)->field($field)->find();
                $number = 'semester_'.$data['week_id'];
                $wherelaboratory['laboratory_id'] = $data['laboratory_id'];
                $week = SemesterModel::where($wherelaboratory)->find();
                if(!empty($week)) {
                    $week -> $number = $semester['id'];
                    $week -> save();
                }
            }
            if($data['week'] == 6) {
                $edit = json_encode($data['new_edit']);
                $res['sun'] = $edit;
                $WeekModel = WeekModel::insert($res);
                $where['sun'] = $edit;
                $field = 'id,sun';
                $semester = WeekModel::where($where)->field($field)->find();
                $number = 'semester_'.$data['week_id'];
                $wherelaboratory['laboratory_id'] = $data['laboratory_id'];
                $week = SemesterModel::where($wherelaboratory)->find();
                if(!empty($week)) {
                    $week -> $number = $semester['id'];
                    $week -> save();
                }
            }
        }
        return json(msg(1, $week));
    }
}
?>